﻿using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static System.Net.WebRequestMethods;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock;


namespace ArticleUploadBatch.WinUI.Services
{

    public class ToutiaoArticleService
    {
        public string PublishUrl { get; set; }
        public string UserName { get; set; }
        public string Pwd { get; set; }
        public IWebDriver Driver { get; set; }

        public ToutiaoArticleService(string userName,string pwd) 
        {
            this.PublishUrl = "https://mp.toutiao.com/profile_v4/graphic/publish";
            this.Driver = new ChromeDriver();
            this.UserName= userName;
            this.Pwd = pwd;
            this.Driver.Manage().Window.Maximize();  //窗口最大化
            //this.Driver.Navigate().GoToUrl(this.PublishUrl);
            this.Driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
        }

        public void UploadOne(string filePath, Action<string> writeLineLogToutiao)
        {
            IWebDriver driver = this.Driver;

            driver.Navigate().GoToUrl(this.PublishUrl);
            //driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
            Thread.Sleep(3000);

            //如果要登陆就先登陆，直达登陆完成
            if (driver.Url.Contains("auth/page/login"))
            {
                //头条登陆
                driver.FindElement(By.CssSelector("#BD_Login_Form > div > article > article > div.web-login-union__footer > div > ul > li:nth-child(4) > span")).Click();
                driver.FindElement(By.Name("normal-input")).SendKeys(this.UserName);//账号
                driver.FindElement(By.CssSelector("#BD_Login_Form > div > article > article > div.web-login-union__login > div.web-login-union__login__form > div.web-login-union__login__form__content > article > div.web-login-confirm-info > span.web-login-confirm-info__checkbox"))
                    .Click();//勾选复选框
                driver.FindElement(By.CssSelector("#BD_Login_Form > div > article > article > div.web-login-union__login > div.web-login-union__login__form > div.web-login-union__login__form__content > article > div.web-login-account-password__password-input-wrapper > div > div > input"))
                    .SendKeys(this.Pwd);//密码

                driver.FindElement(By.CssSelector("#BD_Login_Form > div > article > article > div.web-login-union__login > div.web-login-union__login__form > div.web-login-union__login__form__content > article > div.web-login-account-password__button-wrapper > button"))
                    .Click();//点击登陆

                while (true)
                {
                    if (driver.Url.Contains("auth/page/login"))
                    {
                        Thread.Sleep(2000);
                    }
                    else
                    {
                        writeLineLogToutiao("登陆成功：");
                        break;
                    }
                }
            }

            //确定是发布文章
            if (driver.Url.Contains("graphic/publish"))
            {
                //单个文章上传
                string title = Path.GetFileNameWithoutExtension(filePath);
                writeLineLogToutiao("正在上传文章：" + title);
                //发布文章
                try
                {
                    //是否继续编辑按钮
                    driver.FindElement(By.CssSelector(".byte-drawer-mask"))
                   .Click();
                }
                catch (Exception)
                {
                }            
                if (System.IO.File.Exists(filePath))
                {

                    driver.FindElement(By.CssSelector("#root > div > div > div.publish-editor > div.publish-editor-title-wrapper > div > div > div.title-wrapper > div > div > div > textarea"))
                    .SendKeys(title);//输入标题
                    string content = System.IO.File.ReadAllText(filePath);
                    driver.FindElement(By.CssSelector("#root > div > div > div.publish-editor > div.syl-editor-wrap > div > div.ProseMirror"))
                        .SendKeys(content);//输入文章内容
                    Thread.Sleep(2000);
                    //driver.FindElement(By.CssSelector("#root > div > div > div.publish-footer > div > button:nth-child(3)"))
                    //    .Click();//返回主页
                    driver.Navigate().GoToUrl("https://mp.toutiao.com/profile_v4/manage/draft");

                    Thread.Sleep(1000);
                    writeLineLogToutiao("上传文章成功：" + title);
                }
            }
        }

        public void UploadMany(string[] filePathList,Action<string> writeLineLogToutiao)
        {         
            foreach (string filePath in filePathList)
            {
                this.UploadOne(filePath, writeLineLogToutiao);
            }
            writeLineLogToutiao("所有文章上传完成");
        }

        public void Quit()
        {
            this.Driver.Close();
            this.Driver.Quit();
        }


    }
}
